package com.augurit.common.system.service.auth.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.base.form.ResultForm;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.dao.OmUserDao;
import com.augurit.common.system.entity.LoginConfine;
import com.augurit.common.system.service.auth.ILoginUserService;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.OmUserForm;

/**
 * 登录用户逻辑实现类
 */
@Service
@Transactional
public class LoginUserServiceImpl implements ILoginUserService {
	
	// Service 引用
	@Autowired
	private IOmUserService omUserService;
	@Autowired
	private OmUserDao omUserDao;
	/**
	 * 根据用户登录名获取登录用户的相关信息
	 * @param loginName 用户登录名
	 * @return
	 */
	public LoginUserForm getLoginUserInfo(String loginName){
		LoginUserForm result = new LoginUserForm();
		
		OmUserForm user = omUserService.get(loginName);
		result.setUser(user);
		
		return result;
	}
	
	/**
	 * 检验登录用户的用户名和密码是否正确
	 * @param loginName 用户登录名
	 * @param password 用户密码
	 * @return
	 */
	public boolean isLoginValid(String loginName, String password){
		return omUserService.isValid(loginName, password);
	}
	
	/**
	 * 根据登录用户的用户名和密码获取用户对象，带用户名和密码校验功能
	 * @param loginName 用户登录名
	 * @param password 用户密码
	 * @return 如用户名和密码正确则返回用户对象，否则返回NULL
	 */
	public ResultForm<LoginUserForm> getLoginUserInfo(String loginName, String password){
		ResultForm<LoginUserForm> result = new ResultForm(false);
		
		OmUserForm user = omUserService.getValidUser(loginName, password);
		if(user != null){
			LoginUserForm loginUser = new LoginUserForm();
			loginUser.setUser(user);
			
			result.setSuccess(true);
			result.setResult(loginUser);
		}else{
			result.setErrMsg(OrgConstant.MSG_INVALID_USER);
		}
		
		return result;
	}

	@Override
	public LoginConfine getErrorInfo(String username) {
		String sql=" from LoginConfine where username='"+username+"'";
		List list=this.omUserDao.find(sql);
		if(list.size()>0){
			return (LoginConfine) list.get(0);
		}
		return null;
	}

	@Override
	public void updateErrorNum(LoginConfine confine) {
		Session session=this.omUserDao.getSession();
		session.saveOrUpdate(confine);
		
	}

	@Override
	public void saveErrorInfo(LoginConfine confine) {
		Session session=this.omUserDao.getSession();
		session.saveOrUpdate(confine);
		
		
	}

	@Override
	public void saveErrorInfo(String loginName,boolean flag) {
		LoginConfine confine=this.getErrorInfo(loginName);
		if(flag){
			if(confine!=null){
				int num=confine.getErrorNum()+1;
				confine.setLogintime(new Date());
				confine.setErrorNum(num);
				
			}else{
				confine=new LoginConfine();
				confine.setErrorNum(1);
				confine.setLogintime(new Date());
				confine.setUsername(loginName);
			}
		}else{
			if(confine!=null){
				confine.setErrorNum(0);
				confine.setLogintime(null);
			}
		}
		if(confine!=null){
			Session session=this.omUserDao.getSession();
			session.saveOrUpdate(confine);
		}
		
	}
}
